package chat_client;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class ChatScreen
{
    private final JFrame frame;
    private final JPanel panel;
    private final JScrollPane scrollPane;
    private final JTextArea textArea1;
    private final JLabel label1;
    private final JTextField textField;
    private final JButton button;

    private final MessageIssuing msgIssuing;

    public ChatScreen(MessageIssuing issuing)
    {
	this.msgIssuing = issuing;

	this.frame = new JFrame("fernandosilva.net.br - Chat");
	this.panel = new JPanel();
	this.textArea1 = new JTextArea(10, 60);
	this.textArea1.setEditable(false);
	this.scrollPane = new JScrollPane(this.textArea1);
	this.label1 = new JLabel("Digite uma mensagem:");
	this.textField = new JTextField(60);
	this.button = new JButton("enviar");

	this.frame.setContentPane(this.panel);

	this.panel.add(this.scrollPane);
	this.panel.add(this.label1);
	this.panel.add(this.textField);
	this.panel.add(button);

	class SendMessageListener implements ActionListener
	{
	    public void actionPerformed(ActionEvent e)
	    {
		msgIssuing.send(textField.getText());
		textField.setText("");
	    }
	}

	this.button.addActionListener(new SendMessageListener());

	this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	this.frame.setSize(700, 300);
	this.frame.setVisible(true);
    }

    public void addMessage(String message)
    {
	this.textArea1.append(message + "\n");
    }
}